package jwr.ex;

import java.util.Scanner;

public class WorldGameApp {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int join;
		char firstChar, lastChar;
		int lastIndex;
		String userName[];
		String word = "아버지";
		User ur = new User(word);
		
		System.out.print("게임에 참가하는 인원은 몇명입니까?>>");
		join = sc.nextInt();
		userName = new String[join];
		for(int i = 0 ; i < join; i++){
			System.out.print("참가자의 이름을 입력하세요>>");
			userName[i]=sc.next();
		}
		System.out.println("시작하는 단어는 "+ word+" 입니다.");
		
		for(int i = 0;i<userName.length;i++){
			System.out.println(userName[i]+">>");
			word = sc.next();
			lastIndex = word.length()-1;
			firstChar = word.charAt(0);
			lastChar = word.charAt(lastIndex);
			if(ur.succeed(lastIndex,firstChar,lastChar)){
				if(i == (userName.length-1)){i = -1;}
			}else{
				System.out.println(userName[i]+"님이 졌습니다.");
				break;
			}
			
		}
		/*int i = 0;
		do{
			System.out.println(userName[i]+">>");
			word = sc.next();
			lastIndex = word.length()-1;
			firstChar = word.charAt(0);
			lastChar = word.charAt(lastIndex);
			if(ur.succeed(lastIndex,firstChar,lastChar)){
				if(i == userName.length){i = 0;}
			}else{
				System.out.println(userName[i]+"님이 졌습니다.");
				break;
			}
			i++;
		}while(i<userName.length+1);*/
	}

}

class User{
	char firstChar, lastChar;
	int lastIndex;
	public User(String word) {
		lastIndex = word.length()-1;
		firstChar = word.charAt(0);
		lastChar = word.charAt(lastIndex);
		
	}
	
	boolean succeed(int lastIndex,char firstChar, char lastChar){
		boolean n;
		
		if(this.lastChar==firstChar){
			n = true;
			this.firstChar = firstChar;
			this.lastChar = lastChar;
		}else {
			n = false;
		}
		return n;
	}
}
